<template>
  <div>
    <el-row>
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <el-button type="primary" @click="add">新增</el-button>
        </div>
      </el-col>
      <el-col :span="8" :offset="4">
        <div class="grid-content bg-purple-light">
          <el-input
            placeholder="请输入内容"
            prefix-icon="el-icon-search"
            @keyup.enter.native="onSearch"
            v-model="search"
          ></el-input>
        </div>
      </el-col>
    </el-row>
    <div class="table">
      <template>
        <el-table :data="tableData" height="250" border style="width: 100%">
          <el-table-column type="index" align="center" label="序号" width="80"></el-table-column>
          <el-table-column prop="username" align="center" label="用户名" width="180"></el-table-column>
          <el-table-column prop="password" align="center" label="密码"></el-table-column>
          <el-table-column prop="email" align="center" label="邮箱"></el-table-column>
          <el-table-column prop="mobile" align="center" label="电话"></el-table-column>
          <el-table-column prop="address" align="center" label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="warning"
                @click="handleUpdate(scope.$index, scope.row)"
              >修改</el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </div>

    <!-- 分页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="pageSize"
      :current-page="curPage"
      @current-change="pageChange"
    ></el-pagination>

    <div class="zhezhao" v-show="showModel">
      <el-row>
        <el-col :span="10" :offset="6">
          <div class="box" style="background:#fff">
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span>新增</span>
                <el-button style="float: right; padding: 3px 0" type="text" @click="close">关闭</el-button>
              </div>
              <div>
                <el-form
                  :model="ruleForm"
                  :rules="rules"
                  ref="ruleForm"
                  label-width="100px"
                  class="demo-ruleForm"
                >
                  <el-form-item label="用户名" prop="username">
                    <el-input v-model="ruleForm.username" name="username"></el-input>
                  </el-form-item>
                  <el-form-item label="密码" prop="password">
                    <el-input type="password" v-model="ruleForm.password" name="password"></el-input>
                  </el-form-item>
                  <el-form-item label="邮箱" prop="email">
                    <el-input v-model="ruleForm.email" name="email"></el-input>
                  </el-form-item>
                  <el-form-item label="电话" prop="mobile">
                    <el-input v-model="ruleForm.mobile" name="mobile"></el-input>
                  </el-form-item>
                  <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                  </el-form-item>
                </el-form>
              </div>
            </el-card>
          </div>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
    
</template>

<script>
import { myUserUrl } from "../utile/Url";
import qs from "qs";
import axios from "axios";
export default {
  data() {
    return {
      showModel: false,
      search: "",
      tableData: [],
      total: 0,
      pageSize: 3,
      curPage: 1,
      ruleForm: {
        id: "",
        username: "",
        password: "",
        email: "",
        mobile: ""
      },
      rules: {
        username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
        mobile: [{ required: true, message: "请输入电话", trigger: "blur" }]
      }
    };
  },
  methods: {
    getdata() {
      axios({
        url: myUserUrl,
        method: "get",
        params: {
          search: this.search,
          page:this.curPage
        }
      }).then(res => {
        // console.log(res);
        this.tableData = res.data.data;
        this.total=res.data.total*1
      });
    },
    //点击增加按钮
    add() {
      this.showModel = true;
    },
    //点击关闭按钮
    close() {
      this.showModel = false;
      this.resetForm();
      // this.getdata()
    },
    //提交按钮
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          axios({
            url: myUserUrl,
            method: "post",
            data: qs.stringify(this.ruleForm)
          }).then(res => {
            // console.log(res)
            this.getdata();
            this.close();
          });
        }
      });
    },
    //重置按钮
    resetForm(formName) {
      this.$refs["ruleForm"].resetFields();
    },
    //修改按钮
    handleUpdate(id, row) {
      //   console.log(id, row);
      this.showModel = true;
      //深克隆
      this.ruleForm = JSON.parse(JSON.stringify(row));
    },
    handleDelete(index, row) {
      let id = row.id;
      // let id = []
      // row.forEach(item=>{
      //   id.push(item.id)
      // })
      // id = id.join(',')
      if (confirm("确定删除吗")) {
        axios({
          url: myUserUrl,
          method: "get",
          params: {
            transform: 1,
            id
          }
        }).then(res => {
          // console.log(res);
          this.tableData = res.data.data;
          this.getdata()
        });
      }
    },
    pageChange(currentPage) {
      this.curPage=currentPage
      this.getdata()
    },
    //搜索
    onSearch() {
      // console.log(this.search);
      this.getdata();
    }
  },
  mounted() {
    this.getdata();
  }
};
</script>

<style>
.table {
  margin: 20px 0;
}
.zhezhao {
  position: fixed;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  background: rgb(99, 97, 97);
  z-index: 99;
}
.box {
  margin: 15px 0;
}
</style>
